package com.example.b_308.medical;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.b_308.medical.R;
import com.example.b_308.medical.controller.UserController;
import com.example.b_308.medical.model.Staff;
import com.example.b_308.medical.view.base.BaseActivity;
import com.example.b_308.medical.view.user.IUserView;

/**
 * Created by B-308 on 2017/11/22.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener, IUserView {
    EditText et_phone, et_phone_code, et_doctor_code, et_password1, et_password2;
    ImageView iv_return, iv_phone_code_delete, iv_doctor_code_delete, iv_password1_delete, iv_password2_delete;
    Button but_getCode_again, but_register;
    TextView tv_setting;
    UserController userController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initDate();
        initListener();
    }

    private void initListener() {
        iv_return.setOnClickListener(this);
        iv_phone_code_delete.setOnClickListener(this);
        iv_doctor_code_delete.setOnClickListener(this);
        iv_password1_delete.setOnClickListener(this);
        iv_password2_delete.setOnClickListener(this);
        but_getCode_again.setOnClickListener(this);
        but_register.setOnClickListener(this);
        et_phone_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_phone_code_delete.setVisibility(View.GONE);
                } else {
                    iv_phone_code_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_doctor_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_doctor_code_delete.setVisibility(View.GONE);
                } else {
                    iv_doctor_code_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_password1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_password1_delete.setVisibility(View.GONE);
                } else {
                    iv_password1_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_password2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_password2_delete.setVisibility(View.GONE);
                } else {
                    iv_password2_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initDate() {
        userController = new UserController(this);
    }


    private void initView() {
        iv_return = findView(R.id.iv_return);
        tv_setting = findView(R.id.tv_setting);
        tv_setting.setVisibility(View.GONE);
        et_phone = findView(R.id.et_phone);
        et_phone_code = findView(R.id.et_phone_code);
        et_doctor_code = findView(R.id.et_doctor_code);
        et_password1 = findView(R.id.et_password1);
        et_password2 = findView(R.id.et_password2);
        iv_phone_code_delete = findView(R.id.iv_phone_code_delete);
        iv_doctor_code_delete = findView(R.id.iv_doctor_code_delete);
        iv_password1_delete = findView(R.id.iv_password1_delete);
        iv_password2_delete = findView(R.id.iv_password2_delete);
        but_getCode_again = findView(R.id.but_getCode_again);
        but_register = findView(R.id.but_register);
        iv_phone_code_delete.setVisibility(View.GONE);
        iv_doctor_code_delete.setVisibility(View.GONE);
        iv_password1_delete.setVisibility(View.GONE);
        iv_password2_delete.setVisibility(View.GONE);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_return:
                tent2(Verification_codeActivity.class);
                break;
            case R.id.iv_phone_code_delete:
                et_phone_code.setText("");
                break;
            case R.id.iv_doctor_code_delete:
                et_doctor_code.setText("");
                break;
            case R.id.iv_password1_delete:
                et_password1.setText("");
                break;
            case R.id.iv_password2_delete:
                et_password2.setText("");
                break;
            case R.id.but_getCode_again:
                userController.getCode(et_phone.getText().toString(), 0);
                break;
            case R.id.but_register:
                userController.register(et_phone.getText().toString(), (Number) et_doctor_code.getText(),et_phone_code.getText().toString(),et_password1.getText().toString(),et_password2.getText().toString());
                break;
        }
    }

    @Override
    public void loginSuccess(Staff staff) {

    }

    @Override
    public void registerSuccess() {
        tent2(LoginActivity.class);
    }

    @Override
    public void getCodeSuccess() {

    }
}
